<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/3/28
 * Time: 13:30
 */

namespace front\modules\app\controllers;

use common\helpers\ReturnHelper;
use front\controllers\RestBaseController;
use goods\services\ShopService;
use order\services\OrderUserService;
use users\services\RegionService;
use Yii;
use yii\helpers\ArrayHelper;
use yii\helpers\Json;

class AddressController extends RestBaseController
{

    /**
     *
     */
    public function actionList()
    {
        $data = yii::$app->request->post();
        $param = [
            'getByWeb' => 1,
            'user_id' => $data['user_id'],
            'status' => 1
        ];
        $result = OrderUserService::search($param);
        $addressArr = [];
        if($result['code'] == 0){
            $addressList = $result['result'];
            $addressList = ArrayHelper::toArray($addressList);
            foreach($addressList as $key => $address){
                $add = [];
                $add['id'] = $address['id'];
                $add['name'] = $address['name'];
                $add['mobile'] = $address['mobile'];
                $add['isdefault'] = $address['default'];
                if($address['district'] == 0){
                    $district = $address['dist_cn_char'];
                }else{
                    $district = RegionService::getAddress($address['district']);
                }
                $add['detail'] = $district . $address['address'];
                array_push($addressArr, $add);
            }
        }
        return ReturnHelper::success('success', $addressArr);
    }

    /**
     *
     * $user_id
     * $default
     * $name
     * $district
     * $address
     * $mobile
     */
    public function actionCreate()
    {
        $params = \yii::$app->request->post();
        if(!isset($params['default'])){
            $params['default'] = 0;
        }
        if(!isset($params['address'])){
            return ReturnHelper::error('详细地址不能为空');
        }
        if(!isset($params['user_id'])){
            return ReturnHelper::error('用户ID不能为空');
        }
        $params['default'] = 0 + $params['default'];
        $params['address'] = trim($params['address']);
        $result = OrderUserService::addAddress($params);
        if($result['code'] == 0){
            return ReturnHelper::success('success');
        }
        return ReturnHelper::error($result['message']);
    }

    /**
     *
     * @return array
     */
    public function actionDetail()
    {
        $data = Yii::$app->request->post();
        if(!isset($data['id'])){
            return ReturnHelper::error('ID不能为空');
        }
        $data['getByWeb'] = 1;
        $address = OrderUserService::search($data);
        $address = ArrayHelper::toArray($address['result']);
        $address = $address[0];
        return ReturnHelper::success('success', ['data' => $address]);
    }

    /**
     *
     * @return array
     */
    public function actionUpdate()
    {
        $param = Yii::$app->request->post();
        $param['getByWeb'] = 1;
        if(!isset($param['default'])){
            $param['default'] = 0;
        }
        $param['default'] = 0 + $param['default'];
        if(!isset($param['address'])){
            return ReturnHelper::error('详细地址不能为空');
        }
        $param['address'] = trim($param['address']);
        $info =  OrderUserService::update($param);
        if($info['code'] == 0){
            return ReturnHelper::success('success');
        }
        return ReturnHelper::error($info['message']);
    }

    /**
     * 删除地址
     * @return Json
     */
    public function actionRemove()
    {
        $param = Yii::$app->request->post();
        if(!isset($param['id'])){
            return ReturnHelper::error('id不能为空');
        }

        $data['id'] = $param['id'];
        $data['getByWeb'] = 1;
        $address = OrderUserService::search($data);
        $address = ArrayHelper::toArray($address['result']);
        if($address==1){
            return ReturnHelper::error('默认地址不能删除');
        }

        $result = OrderUserService::delete($param);
        if($result['code'] == 0){
            return ReturnHelper::success('success', $result['result']);
        }else{
            return ReturnHelper::error($result['msg']);
        }

    }

    /**
     * 用户自提地址列表
     * @return string
     */
    public function actionSelfAddress()
    {
        $fromInfo = Yii::$app->request->post();
        $address = ShopService::getAllAddress();
        $address = ArrayHelper::toArray($address);
        $data = ['list' => $address,'fromInfo' => $fromInfo];
        return ReturnHelper::success('success', $data);
    }

}